<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Model\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Input;

class LoginController extends Controller
{
    public function login()
    {
        if($input = Input::all()){
            if(strtolower($input['code'])!=strtolower(session('la_code'))){
                return back()->with('errors', '验证码错误');
            }
            $user = User::first();
            if($user->user_name!=$input['user_name'] || $user->user_pass!=md5($input['user_pass'])){
                return back()->with('errors', '账号或密码错误');
            }
            session(['la_user'=>$user]);
            return redirect('admin/index');
        }else{
            return view('admin.login');
        }

    }
    public function code()
    {
        $builder = new CaptchaBuilder();
        $builder->build();
        session(['la_code'=>$builder->getPhrase()]);
        header('Content-type: image/jpeg');
        $builder->output();
    }
    public function quit()
    {
        session(['la_user' => null]);
        return redirect('admin/login');
    }
}
